package com.member.mp.web;

import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.member.common.core.domain.R;
import com.member.mp.web.user.SupperController;
import com.member.shop.dto.OrderPayDto;
import com.member.shop.dto.WxPayDto;
import com.member.shop.service.IPayService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wangxin
 * @Description
 * @date 2024/1/15 12:02
 **/
@Slf4j
@RestController
@Api(value = "支付接口", tags = {"支付接口"})
@RequestMapping("/wx/pay")
public class PayController extends SupperController {

    @Resource
    private IPayService payService;

    /**
     * 微信支付 app
     *
     * @param orderPayDto
     * @return
     */
    @PostMapping("/payOrder")
    private R<WxPayAppOrderResult> payOrder(@RequestBody OrderPayDto orderPayDto) {
        try {
            return R.ok(payService.wxAppPay(WxPayDto.builder()
                    .payId(orderPayDto.getPaymentId())
                    .orderId(orderPayDto.getOrderCode())
                    .tenantId(getTenantId())
                    //.openId(getLoginUser().getOpenId())//APP 不需要传这个参数
                    .totalFee(orderPayDto.getActualPaymentAmount())
                    .spbillCreateIp("127.0.0.1")
                    .notifyUrl("/mp-api/wx/payment/callbackByOrder").build()));
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }

}
